package com.example.livingadminmaster.controller;

import com.example.livingadminmaster.pojo.Dormitory;
import com.example.livingadminmaster.pojo.StatusRequest;
import com.example.livingadminmaster.service.DormitoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dormitory")
public class DormitoryController {
    @Autowired
    private DormitoryService dormitoryService;

    /**
     * 获得所有宿舍管理员信息
     * @return
     */
    @ResponseBody
    @GetMapping("/AllDormitories")
    public List<Dormitory> getAllDormitories() {
        return dormitoryService.getAllDormitories();
    }

    /**
     * 根据宿舍管理员ID获得信息
     * @param dormitory_admin_id
     * @return
     */
    @GetMapping("/DormitoryId/{dormitory_admin_id}")
    public Dormitory getDormitoryId(@PathVariable("dormitory_admin_id") long dormitory_admin_id) {
        return dormitoryService.getDormitoryId(dormitory_admin_id);
    }

    /**
     * 根据宿舍管理员用户名获得信息
     * @param username
     * @return
     */
    @GetMapping("/DormitoryUsername")
    public Dormitory getDormitoryUsername(@RequestParam String username) {
        return dormitoryService.getDormitoryUsername(username);
    }

    /**
     * 根据宿舍管理员性别获得信息
     * @param gender
     * @return
     */
    @GetMapping("/DormitoryGender")
    public List<Dormitory> getDormitoryGender(@RequestParam String gender) {
        return dormitoryService.getDormitoryGender(gender);
    }

    /**
     * 根据宿舍楼获得信息
     * @param dormBuilding
     * @return
     */
    @GetMapping("/DormitoryDormBuilding")
    public List<Dormitory> getDormitoryDormBuilding(@RequestParam String dormBuilding) {
        return dormitoryService.getDormitoryDormBuilding(dormBuilding);
    }

    /**
     * 添加宿舍管理员信息
     * @param dormitory
     */
    @PostMapping("/addDormitory")
    @ResponseBody
    public void addDormitory(@RequestBody Dormitory dormitory){
        dormitoryService.addDormitory(dormitory);
    }

    /**
     * 删除宿舍管理员信息
     * @param dormitory_admin_id
     */
    @DeleteMapping("/deleteDormitory/{dormitory_admin_id}")
    @ResponseBody
    public void deleteDormitory(@PathVariable("dormitory_admin_id") long dormitory_admin_id){
        dormitoryService.deleteDormitory(dormitory_admin_id);
    }

    /**
     * 更新宿舍管理员信息
     * @param dormitory_admin_id
     * @param dormitory
     * @return
     */
    @PutMapping("/updateDormitory/{dormitory_admin_id}")
    public String updateDormitory(@PathVariable long dormitory_admin_id, @RequestBody Dormitory dormitory) {
        if (dormitory.getDormitoryAdminId() != dormitory_admin_id) {
            return "Failed to update dormitory: ID mismatch";
        }
        int result = dormitoryService.updateDormitory(dormitory);
        if (result > 0) {
            return "更新成功";
        } else {
            return "更新失败";
        }
    }

    /**
     * 更新宿舍管理员状态
     * @param dormitory_admin_id
     * @param statusRequest
     */
    @PostMapping("/{dormitory_admin_id}/updateDormitoryStatus")
    public void updateDormitoryStatus(@PathVariable long dormitory_admin_id,@RequestBody StatusRequest statusRequest) {
        dormitoryService.updateDormitoryStatus(dormitory_admin_id, statusRequest.getStatus());
    }
}
